package com.llmcu.graphic.designpatterns.mediator;

import javafx.scene.control.PasswordField;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

/**
 * xxxx
 *
 * @author liuling
 * @date 2024/7/21 9:58
 */
public class LoginFrame extends JFrame implements Mediator, WindowListener {

    LoginCheckBox guestLoginTypeCheckBox;
    LoginCheckBox userLoginTypeCheckBox;
    LoginTextField nameTextField;
    LoginPasswordField passwordField;
    LoginButton okButton;
    LoginButton cancelButton;

    public LoginFrame(String title) throws HeadlessException {
        super(title);
        this.setLayout(new GridLayout(4,2));
        Container contentPane = this.getContentPane();

        ButtonGroup buttonGroup = new ButtonGroup();
         guestLoginTypeCheckBox = new LoginCheckBox("guest",false);
         userLoginTypeCheckBox = new LoginCheckBox("user",true);
        guestLoginTypeCheckBox.setMediator(this);
        userLoginTypeCheckBox.setMediator(this);
        buttonGroup.add(guestLoginTypeCheckBox);
        buttonGroup.add(userLoginTypeCheckBox);

        JLabel nameLabel = new JLabel("name:");
        nameTextField = new LoginTextField(20);
        nameTextField.setMediator(this);

        JLabel passwordLabel = new JLabel("password:");
        passwordField = new LoginPasswordField(20);
        passwordField.setMediator(this);

        okButton = new LoginButton("OK");
        cancelButton = new LoginButton("CANCEL");
        okButton.setMediator(this);
        cancelButton.setMediator(this);

        contentPane.add(guestLoginTypeCheckBox);
        contentPane.add(userLoginTypeCheckBox);
        contentPane.add(nameLabel);
        contentPane.add(nameTextField);
        contentPane.add(passwordLabel);
        contentPane.add(passwordField);
        contentPane.add(okButton);
        contentPane.add(cancelButton);

        this.addWindowListener(this);
        guestLoginTypeCheckBox.addItemListener(guestLoginTypeCheckBox);
        userLoginTypeCheckBox.addItemListener(userLoginTypeCheckBox);
       nameTextField.getDocument().addDocumentListener(nameTextField);
       passwordField.getDocument().addDocumentListener(passwordField);


        this.pack();
        this.show();
    }

    @Override
    public void colleagueChanged() {
        boolean isGuest = guestLoginTypeCheckBox.isSelected();
        if(isGuest){
            nameTextField.setEnabled(false);
            passwordField.setEnabled(false);
            okButton.setEnabled(true);
        }else{
            nameTextField.setEnabled(true);
            nameTextFieldChanged();

        }

    }

    private void nameTextFieldChanged() {
        int nameTextLength = nameTextField.getText().length();
        if(nameTextLength>0){
            passwordField.setEnabled(true);
           passwordTextChanged();
        }else{
            passwordField.setEnabled(false);
            okButton.setEnabled(false);

        }
    }

    private void passwordTextChanged() {
        int nameTextLength = passwordField.getText().length();
        if(nameTextLength>0){
            okButton.setEnabled(true);
        }else{
            okButton.setEnabled(false);

        }

    }

    @Override
    public void windowOpened(WindowEvent e) {

    }

    @Override
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }

    @Override
    public void windowClosed(WindowEvent e) {

    }

    @Override
    public void windowIconified(WindowEvent e) {

    }

    @Override
    public void windowDeiconified(WindowEvent e) {

    }

    @Override
    public void windowActivated(WindowEvent e) {

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }
}
